package com.heer.gymnasium.module.fellow.views;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.heer.amap.utils.ToastUtil;
import com.heer.gymnasium.R;
import com.heer.gymnasium.api.ResponseData.ApiResponse;
import com.heer.gymnasium.api.callback.BaseCallback;
import com.heer.gymnasium.base.BaseActivity;
import com.heer.gymnasium.constant.Type;
import com.heer.gymnasium.module.fellow.beans.MatchBean;
import com.heer.gymnasium.module.login.views.LoginActivity;
import com.heer.gymnasium.module.order.beans.OrderResponseBean;
import com.heer.gymnasium.module.order.views.OrderActivity;
import com.heer.gymnasium.module.reservation.beans.ReservationGridBean;
import com.heer.gymnasium.module.team.beans.TeamBean;
import com.heer.gymnasium.module.team.beans.TeamResponseBean;
import com.heer.gymnasium.module.team.views.TeamDetailActivity;
import com.heer.gymnasium.util.StringUtils;
import com.zhihuiyun.library.commonadapter.QuickTypeAdapter;
import com.zhihuiyun.library.commonadapter.ViewHolder;
import com.zhihuiyun.library.switchbutton.OnSwitchListener;
import com.zhihuiyun.library.switchbutton.SwitchButton;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by yan
 * on 2018/2/23
 * 发起拼单
 */

public class InitiateMatchActivity extends BaseActivity {

    private static final int RES_ID_LAYOUT = R.layout.layout_initiate_match;
    private static final int RES_ID_MONEY = R.id.layout_initiate_match_money_tv;
    private static final int RES_ID_LIST = R.id.layout_initiate_match_lv;
    private static final int RES_ID_EXPLAIN = R.id.layout_initiate_match_explain_et;
    private static final int RES_ID_NEXT = R.id.layout_initiate_match_next_tv;
    private static final int RES_ID_MORE = R.id.layout_initiate_match_more_fl;
    private static final int RES_ID_MCONTENT_LL = R.id.layout_initiate_match_explain_ll;
    private static final int RES_ID_OPEN_RL = R.id.layout_initiate_match_open_rl;
    private static final int RES_ID_OPEN = R.id.layout_initiate_match_switch_button;

    private static final int RES_ID_ITEM = R.layout.item_check_box;


    @BindView(RES_ID_EXPLAIN)
    EditText explainEditText;
    @BindView(RES_ID_MONEY)
    TextView moneyTextView;
    @BindView(RES_ID_LIST)
    ListView listView;
    @BindView(RES_ID_MORE)
    View vMore;
    @BindView(RES_ID_MCONTENT_LL)
    LinearLayout conentView;
    @BindView(RES_ID_OPEN_RL)
    RelativeLayout rlOpen;
    @BindView(RES_ID_OPEN)
    SwitchButton switchButton;

    private QuickTypeAdapter<TeamBean> adapter;
    private List<TeamBean> data;

    private String tid;
    private float totalPrice;
    private int pageNo;

    private InitiateMatchActivity $this;
    private final static int RRESULT_CODE_MATCH = 0x8;
    private String from;
    private boolean isOpen = false;

    @Override
    protected void init() {
        super.init();
        $this = this;
        setContentView(RES_ID_LAYOUT);
        data = new ArrayList<>();
        totalPrice = getIntent().getFloatExtra(EXTRA_DATA, 0f);
        from = getIntent().getStringExtra(EXTRA_FROM);
    }

    @Override
    protected void initView() {
        super.initView();
        if (!TextUtils.isEmpty(from)) {
            conentView.setVisibility(View.GONE);
            rlOpen.setVisibility(View.GONE);
        }
        adapter = new QuickTypeAdapter<TeamBean>(this, data, RES_ID_ITEM) {
            @Override
            public void bindData4View(ViewHolder holder, final TeamBean data, int type, int pos) {
                CheckBox checkBox = (CheckBox) holder.getParentView();
                checkBox.setText(data.getTeamName());

                if (data.isSelected()) {
                    checkBox.setChecked(true);
                } else {
                    checkBox.setChecked(false);
                }

                holder.getParentView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        for (TeamBean item : $this.data) {
                            item.setSelected(false);
                        }
                        data.setSelected(true);
                        tid = data.getId();
                        adapter.notifyDataSetChanged();
                    }
                });
            }
        };
        listView.setAdapter(adapter);
        getMyCorpsList();
        moneyTextView.setText(String.format("%.2f元/队", totalPrice));
        switchButton.setOnSwitchListener(new OnSwitchListener() {
            @Override
            public void onSwitchChange(int currentStatus) {
                if (currentStatus == SwitchButton.OPEN) {
                    isOpen = true;
                } else {
                    isOpen = false;
                }
            }
        });
    }

    private void getMyCorpsList() {
        mAppHelper.apiService().getMyCorpsList(String.valueOf(pageNo)).enqueue(new BaseCallback<ApiResponse<TeamResponseBean>>() {
            @Override
            public void onSuccess(Call<ApiResponse<TeamResponseBean>> call, Response<ApiResponse<TeamResponseBean>> response) {
                if (pageNo == 1) {
                    data.clear();
                }
                pageNo++;
                if (StringUtils.toInt(response.body().getData().getTotal()) <= pageNo) {
                    vMore.setVisibility(View.GONE);
                } else {
                    vMore.setVisibility(View.VISIBLE);
                }
                data.addAll(TeamBean.dataChange(response.body().getData().getData()));
                if (data.size() > 0) {
                    data.get(0).setSelected(true);
                    tid = data.get(0).getId();
                }
                adapter.notifyDataSetChanged();
            }
        });
    }

    @OnClick(RES_ID_MORE)
    public void loadMore() {
        getMyCorpsList();
    }

    @OnClick(RES_ID_NEXT)
    public void nextOnClick() {
        if (TextUtils.isEmpty(from) && TextUtils.isEmpty(explainEditText.getText().toString().trim())) {
            ToastUtil.show($this, "说明信息不能为空");
            return;
        }
        if (TextUtils.isEmpty(from)) {
            Intent intent = new Intent();
            intent.putExtra(EXTRA_ID, tid);
            intent.putExtra(EXTRA_DATA, explainEditText.getText().toString());
            if (isOpen) {
                intent.putExtra(EXTRA_TYPE, "1");
            } else {
                intent.putExtra(EXTRA_TYPE, "0");
            }
            setResult(RESULT_OK, intent);
        } else {
            Intent intent = new Intent();
            intent.putExtra(EXTRA_ID, tid);
            setResult(RRESULT_CODE_MATCH, intent);
        }
        finish();
    }


    public static void startActivity(Activity context, int requestCode, float price) {
        Intent mIntent = new Intent(context, InitiateMatchActivity.class);
        mIntent.putExtra(EXTRA_DATA, price);
        context.startActivityForResult(mIntent, requestCode);
    }


    public static void startActivity(Activity context, int requestCode, float price, String from) {
        Intent mIntent = new Intent(context, InitiateMatchActivity.class);
        mIntent.putExtra(EXTRA_DATA, price);
        mIntent.putExtra(EXTRA_FROM, from);
        context.startActivityForResult(mIntent, requestCode);
    }


}
